<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\TongjiService;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class TongjiController extends BaseController
{
    //工时录入

    //个人
    public function person(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['uid'] = $request->post('uid', 0);
        $res = TongjiService::person($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data'], $res['total']);
    }

    //部门领导
    public function item(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['uid'] = $request->post('uid', 0);
        $res = TongjiService::item($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data'], $res['total']);
    }

    //审核员
    public function all(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['role_id'] = $request->post('role_id', 0);
        $res = TongjiService::all($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data'], $res['total']);
    }

    //环比
    public function huan(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $res = TongjiService::huan($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data'], $res['total']);
    }

    //图
    public function tu(Request $request)
    {
        $res = TongjiService::tu();
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //选项
    public function getOption()
    {
        $res = TongjiService::getOption();
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //测试导出
    public function excelTest(){
        $cellData = [
            ['学号','姓名','成绩'],
            ['10001','AAAAA','99'],
            ['10002','BBBBB','92'],
            ['10003','CCCCC','95'],
            ['10004','DDDDD','89'],
            ['10005','EEEEE','96'],
        ];
        Excel::create('学生成绩',function($excel) use ($cellData){
            $excel->sheet('score', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xlsx');
    }

//    //示例：某员工8月份的工时统计：研发工时：165.5h  销售工时32h  管理工时：22.5h合计：220h
//    public function staffMonthOut(Request $request)
//    {
//        $depart_id = $request->get('deart_id', 0);
//        $staff_id = $request->get('staff_id', 0);
//        $year = $request->get('year', date('Y', time()));
//        $month = $request->get('month', date('m', time()));
////        if (!$staff_id) {
////            return self::toJson(-2, '员工必须的');
////        }
//        if (!$year) {
//            return self::toJson(-2, '年份必须的');
//        }
//        if (!$month) {
//            return self::toJson(-2, '月份必须的');
//        }
//        $res = TongjiService::staffMonthList($depart_id, $staff_id, $year, $month);
//        if ($res['code'] < 0) {
//            return self::toJson(-2, $res['msg']);
//        }
////        print_r($res); exit;
//        //return self::toJson(0, '', $res['data']);
//        $fname = $res['data']['staff_name'].$month.'月工时汇总';
//        $cellData = $res['data']['data'];
//        //ddd($fname, $cellData);
//        Excel::create($fname,function($excel) use ($cellData){
//            $excel->sheet('sheet', function($sheet) use ($cellData){
//                $sheet->rows($cellData);
//            });
//        })->export('xlsx');
//    }

    //示例：某员工8月份的工时统计：研发工时：165.5h  销售工时32h  管理工时：22.5h合计：220h
    public function staffMonthOut(Request $request)
    {
        $depart_id = $request->get('depart_id', 0);
        $staff_id = $request->get('staff_id', 0);
        $year = $request->get('year', date('Y', time()));
        $month = $request->get('month', date('m', time()));
        if (!$depart_id) {
            return self::toJson(-2, '部门必须的');
        }
        if (!$year) {
            return self::toJson(-2, '年份必须的');
        }
        if (!$month) {
            return self::toJson(-2, '月份必须的');
        }
        $res = TongjiService::staffMonthList($depart_id, $staff_id, $year, $month);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        //ddd($res['data']['data']);
        //print_r($res['data']); exit;
        //return self::toJson(0, '', $res['data']);
        $fname = $res['data']['depart_name'].$month.'月工时汇总';
        $cellData = $res['data']['data'];
        //ddd($fname, $cellData);
        Excel::create($fname,function($excel) use ($cellData){
            $excel->sheet('sheet', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xlsx');
    }
}